package com.lc.config;

import com.lc.adapter.HttpAdapter;
import com.lc.handler.ProtocolHandler;
import com.lc.mapper.Host;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author liuc
 * @date 2021/5/5
 * @description 全局配置类
 **/
public class Configuration {
    private int port;
    //host的配置
    private Map<String, HttpAdapter> adapterMap = new HashMap<>();
    private Map<String, Host> hostMap = new HashMap<>();
    //线程池
    private ThreadPoolExecutor executor = ExecutorFactory.defaultExecutor();
    private ProtocolHandler protocolHandler;

    public Configuration() {
        protocolHandler = new ProtocolHandler(this);
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public ThreadPoolExecutor getExecutor() {
        return executor;
    }

    public void setExecutor(ThreadPoolExecutor executor) {
        this.executor = executor;
    }

    public void addAdapter(HttpAdapter httpAdapter, String hostName){
        adapterMap.put(hostName,httpAdapter);
    }

    public HttpAdapter getAdapter(String hostName){
        HttpAdapter httpAdapter = adapterMap.get(hostName);
        if(httpAdapter == null){
            httpAdapter = new HttpAdapter(this, hostName);
            adapterMap.put(hostName,httpAdapter);
        }
        return httpAdapter;
    }

    public ProtocolHandler getProtocolHandler() {
        return protocolHandler;
    }

    public void setProtocolHandler(ProtocolHandler protocolHandler) {
        this.protocolHandler = protocolHandler;
    }
}
